<?php

namespace app\api\controller;

use app\common\BaseHelper;
use app\admin\model\Activity as ActivityMdl;
use app\admin\model\Joinactivity;

use think\Db;
use think\Exception;
use think\Request;
class Activity extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function getList()

    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->type)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'type 不能为空');
        }
        if (!in_array($param->type, [1, 2])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'type 格式不正确');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'isDelete' => 2
        ];
        if($param->type == 1){
            // 进行中/未开始
            $where['endTime'] = ['egt', time()];
        }else{
            // 已结束
            $where['endTime'] = ['lt', time()];
        }

        $fields = 'id, title, imgUrl, deadline , endTime';

        $result = ActivityMdl::where($where)->limit($start, $length)->field($fields)->select();
        $total = count($result);

        if (empty($result)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到活动');
        }

        foreach ($result as &$item) {
            // 参与人数
            $Joinactivitys = Joinactivity::where(['activityId' => $item['id']])->count();
            $item['Joinactivity'] = $Joinactivitys;
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }
    /**
     * 获取活动详情
     *
     * @return \think\Response
     */
    public function getDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->activityId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'activityId 不能为空');
        }
        if (empty($param->loginId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'loginId 不能为空');
        }

        $activityId = $param->activityId;
        $userId = $param->loginId;
        $where = [
            'id' => $activityId
        ];
        $fields = 'id, title, imgUrl, content, startTime, endTime, address, deadline, quota';

        $item = ActivityMdl::where($where)->field($fields)->find();

        if(empty($item)){
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到相关活动');
        }


        $joinActivityList = Joinactivity::where(['activityId' => $activityId])->field('createDate,userAvatar as avatar,userName as name')->select();
        $item['joinActivityList'] = $joinActivityList;
        // 是否已报名
        $joinInfo = Joinactivity::where(['activityId' => $activityId, 'userId' => $userId])->find();
        if($joinInfo){
            $isJoin = 1;
        }else{
            $isJoin = 2;
        }
        $item['isJoin'] = $isJoin;

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
    }

    /**
     * 报名活动
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function joinActivity()
    {
        $param = self::getHttpParam();

        if (empty($param->activityId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'activityId 不能为空');
        }
        if (empty($param->loginId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'loginId 不能为空');
        }
        if (empty($param->userName)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userName 不能为空');
        }
        if (empty($param->userAvatar)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userAvatar 不能为空');
        }
        if (empty($param->mobile)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'mobile 不能为空');
        }

        $activityId = $param->activityId;
        $userId = $param->loginId;
        $mobile = $param->mobile;
        $joinactivityInfo = Joinactivity::where(['activityId' => $activityId, 'userId' => $userId,'mobile'=>$mobile])->find();

        if($joinactivityInfo){
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '您已报名过，不能再报名');
        }
        $where = [
            'id' => $activityId
        ];

        $sql = \app\admin\model\Activity::where($where)->find();

        $joinNum = Joinactivity::where($where)->select();
        $qouta = $sql['quota'];
        $JoinNum = count($joinNum);

        if ($qouta < $JoinNum + 1)
        {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '报名人数已达上限');
        }
        $data = [
            'id' => BaseHelper::getUUID(),
            'createDate' => time(),
            'activityId' => $activityId,
            'userId' => $userId,
            'userName' => $param->userName,
            'userAvatar' => $param->userAvatar,
            'mobile' => $param->mobile,
        ];
        $result = Joinactivity::create($data);
        if(!$result){
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '提交失败');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '提交成功');
    }

    /*
     * 取消报名活动
     * */
    public function offActivity()
    {
        $param = self::getHttpParam();
        if (empty($param->userId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'userId不能为空');
        }
        if (empty($param->activityId)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'activityId不能为空');
        }
        $where = [
            'activityId' => $param->activityId,
            'userId'     => $param->userId
        ];
        $result = Joinactivity::where($where)->delete();
        if(!$result){
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '取消失败');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, '取消成功');
    }
}
